package com.darrenchan.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2020-04-19 17:53
 */
public class TwoSum {

    Map<Integer, Integer> map;
    public TwoSum() {
        map = new HashMap<>();
    }

    /**
     * @param number: An integer
     * @return: nothing
     * O(1)时间复杂度
     */
    public void add(int number) {
        if(map.containsKey(number)) {
            map.put(number, map.get(number) + 1);
        } else {
            map.put(number, 1);
        }
    }

    /**
     * @param value: An integer
     * @return: Find if there exists any pair of numbers which sum is equal to the value.
     * O(n)时间复杂度
     */
    public boolean find(int value) {
        for (Integer number : map.keySet()) {
            if(map.containsKey(value - number)) {
                if(number == value - number) {
                    if(map.get(number) > 1) return true;
                } else {
                    return true;
                }
            }
        }
        return false;
    }
}
